package com.kfm.base.arrays;

public class Exer03 {

    public static void main(String[] args) {
        // 设 names 数组中存放学生姓名
        String[] names = {"安琪拉", "王昭君", "蔡文姬", "妲己", "张良"};
        // 设 courses 数组中依次存放三门课程的名称
        String[] courses = {"C++", "Java", "Python"};
        // 设 scores 数组中依次存储的是 names 数组中各个学生的 C++ 、Java 、Python 课程的成绩
        int[][] scores = {
                {90, 89, 75},
                {59, 40, 100},
                {100, 99, 80},
                {80, 61, 61},
                {60, 100, 99},
        };

        // 按照 java 成绩排序
//        for (int i = 0; i < scores.length - 1; i++) { // 控制排序的次数
//            for (int j = 0; j < scores.length - i - 1; j++) {// 两两比较的次数
//                if (scores[j][1] < scores[j + 1][1]){
//                    // 成绩交换
//                    int[] temp = scores[j];
//                    scores[j] = scores[j + 1];
//                    scores[j + 1] = temp;
//
//                    // 名字交换
//                    String t = names[j];
//                    names[j] = names[j + 1];
//                    names[j + 1] = t;
//                }
//            }
//        }


        // 根据总成绩排序
        int[] sumArr = new int[scores.length];
        for (int i = 0; i < scores.length; i++) {
            int sum = 0;
            for (int j = 0; j < scores[i].length; j++) {
                sum += scores[i][j];
            }
            sumArr[i] = sum;
        }

        // 选择排序(降序)
        for (int i = 0; i < scores.length - 1; i++) {
            int max = i;
            for (int j = i + 1; j < scores.length; j++) {
                if (sumArr[j] > sumArr[max]){
                    max = j;
                }
            }
            int temp = sumArr[i];
            sumArr[i] = sumArr[max];
            sumArr[max] = temp;

            String n = names[i];
            names[i] = names[max];
            names[max] = n;

            int[] s = scores[i];
            scores[i] = scores[max];
            scores[max] = s;
        }


        // 输出每个人的各科成绩  安琪拉： c++=90  java=89   python=75
        for (int i = 0; i < names.length; i++) {
            System.out.print(names[i] + ": ");
            for (int j = 0; j < scores[i].length; j++) {
                System.out.print(courses[j] + "=" + scores[i][j] + "\t");
            }
            System.out.println(sumArr[i]);
            System.out.println();
        }

    }
}
